package com.jbm.jbmsupplier.activity.address;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weather.myframe.PopupSelectImage;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.AllActivityApp;
import com.jbm.jbmsupplier.activity.address.audiovideo.AudioRecorder;
import com.jbm.jbmsupplier.activity.address.audiovideo.CamerActivity;
import com.jbm.jbmsupplier.activity.address.presenter.MyAddressReClass;
import com.jbm.jbmsupplier.activity.address.view.MyAddressViewInterface;
import com.jbm.jbmsupplier.activity.repairs.UserAddress;
import com.jbm.jbmsupplier.net.FixModel;
import com.jbm.jbmsupplier.net.NetAddRess;
import com.jbm.jbmsupplier.net.busheven.UtlisNetWork;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.utlis.ScreenAsset;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.zp.IndexBar.widget.RecordButton;
import com.jbm.onehome.iface.GoodsClassEntity;
import com.jbm.onehome.iface.IGeoinfoCtrl;
import com.jbm.onehome.iface.Media;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * Created by 家帮忙 on 2016/9/26.
 */
public class MyAddRessRepair extends AllActivityApp implements MyAddressViewInterface {

    @InjectView(R.id.release_desc)
    EditText releaseDesc;
    @InjectView(R.id.recy_repairs_pho)
    RecyclerView recyRepairsPho;
    @InjectView(R.id.image_repairs_zhaopian)
    ImageView imageRepairsZhaopian;
    @InjectView(R.id.baoxiu_fixCategory)
    Spinner baoxiuFixCategory;
    @InjectView(R.id.fix_commit)
    ImageView fixCommit;
    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.toolbar_image)
    ImageView toolbarImage;
    @InjectView(R.id.to_custro)
    Toolbar toCustro;
    @InjectView(R.id.baoxiu_sydzgd)
    TextView baoxiuSydzgd;
    @InjectView(R.id.address_name)
    EditText addressName;
    @InjectView(R.id.address_mobile)
    EditText addressMobile;
    @InjectView(R.id.address_selectAddress)
    Spinner addressSelectAddress;
    @InjectView(R.id.address_selectXiaoQu)
    Spinner addressSelectXiaoQu;
    @InjectView(R.id.ln_XiaoQu)
    LinearLayout lnXiaoQu;
    @InjectView(R.id.address_selectLouMen)
    Spinner addressSelectLouMen;
    @InjectView(R.id.ln_louMen)
    LinearLayout lnLouMen;
    @InjectView(R.id.address_selectHuHao)
    Spinner addressSelectHuHao;
    @InjectView(R.id.ln_huHao)
    LinearLayout lnHuHao;
    @InjectView(R.id.address_et)
    EditText addressEt;
    @InjectView(R.id.image_repairs_shipin)
    ImageView imageRepairsShipin;
    @InjectView(R.id.image_repairs_yuyins)
    RecordButton imageRepairsYuyins;
    private ArrayList<HashMap<String, String>> listrecys;
    HashMap<String, String> map;
    private final int MEDIAIPC = 0003;
    String name = "加载中...";
    private int mode;
    private int screenWidth;
    private File file;
    private DialogLoading baseLoader;
    private int fixCategory;
    private String category;
    private NetAddRess netAddRess;
    private SharedPreferActivity sharedPreferActivity;
    UserAddress date;
    private AddressModel model;
    private FixModel modell;
    private MyAddressReClass myAddressReClass;


    private DialogLoading dialogs;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MEDIAIPC: {
                    Media medi = (Media) msg.obj;
                    if (medi.m_code == 200) {
                        Toast.makeText(MyAddRessRepair.this, "报修成功", Toast.LENGTH_SHORT).show();
                        sharedPreferActivity.setBoolearorder(true);
                        if (dialogs != null) {
                            dialogs.dismiss();
                        }
                        finish();
                    } else {
                        if (dialogs != null) {
                            dialogs.dismiss();
                        }
                        hint(medi.m_info);
                    }
                    break;
                }
                //报修完回调
                case NetAddRess.FIXCATEERROR://报修分类的二级分类
                    GoodsClassEntity obj1 = (GoodsClassEntity) msg.obj;
                    if (obj1.m_code == 200) {
                        if (dialogs != null) {
                            dialogs.dismiss();
                        }
                        modell.setDate(baoxiuFixCategory, obj1.getGoodsClassEntityList());
                    }
                    break;
                //获取地址
                case NetAddRess.ADDRESSCENGJIERROR: {//获取详细地址
                    IGeoinfoCtrl result2 = (IGeoinfoCtrl) msg.obj;
                    model.dismissLoader();
                    if (result2.GIlist != null) {
                        model.setDate(result2.GIlist, addressSelectAddress);
                        addressEt.setVisibility(View.GONE);
                    } else {
                        model.setClickType(model.getClickType() - 1);
                        addressEt.setVisibility(View.VISIBLE);
                    }
                    initHidden();
                    break;
                }
            }
            super.handleMessage(msg);
        }
    };
    File mTmpFile;
    private String addresss;
    private int ids;

    @OnClick({R.id.teleft, R.id.image_repairs_zhaopian, R.id.baoxiu_sydzgd, R.id.fix_commit, R.id.image_repairs_yuyins, R.id.image_repairs_shipin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                finish();
                break;
            case R.id.image_repairs_zhaopian:
                Intent intent = new Intent(MyAddRessRepair.this, PopupSelectImage.class);
                startActivityForResult(intent, 100);
                break;
//            case R.id.baoxiu_sydzgd://添加地址页面
//                Intent intent1 = new Intent(MyAddRessRepair.this, ManageAddressActivity.class);
//                intent1.putExtra("category", category);
//                startActivityForResult(intent1, 102);
//                break;
            case R.id.fix_commit:
                //创建订单
                if (!releaseDesc.getText().toString().equals("")) {//描述的内容
                    addresss = model.getAddress(addressEt);//地址
                    ids = modell.getGoods_code();
                    if (mode == 0 || mode == 1) {
                        if (releaseDesc.getText().length() > 100) {
                            hint("报修内容过多请修改后重试");
                            return;
                        } else if (addressName.getText().toString().equals("")) {//用户名
                            hint("用户名不能为空");
                            return;
                        } else if (addressMobile.getText().toString().equals("")) {//手机号
                            hint("手机号不能为空");
                            return;
                        } else if (addressMobile.getText().toString().length() != 11) {//手机号
                            hint("请核实您的手机号码");
                            return;
                        } else if (addresss.isEmpty() || addresss.length() < 6) {
                            hint("请输入正确的地址");
                            return;
                        } else {
                            myAddressReClass.SubmitMassage();//提交信息
                        }

                    } else if (mode == -1) {
                        hint("网络无连接，请稍后再试！");
                    }

                } else {
                    hint("报修内容不能为空并且不能太少内容");
                }
                break;
            case R.id.image_repairs_yuyins:

                break;
            case R.id.image_repairs_shipin:
                Intent intents = new Intent(MyAddRessRepair.this, CamerActivity.class);
                startActivityForResult(intents, 200);
                break;
        }

    }

    @Override
    public String getDesc() {
        return releaseDesc.getText().toString();
    }

    @Override
    public String getUser() {
        return addressName.getText().toString();
    }

    @Override
    public String getPhoto() {
        return addressMobile.getText().toString();
    }

    @Override
    public String getAddress() {
        return addresss;
    }

    @Override
    public int getMaintain() {
        return ids;
    }

    @Override
    public ArrayList<HashMap<String, String>> getFile() {
        return listrecys;
    }

    @Override
    public RecyclerView getrecycler() {
        return recyRepairsPho;
    }


    @Override
    public int setContentView() {
        return R.layout.repairs_main_baoxiu;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 100) {
            file = (File) data.getSerializableExtra("file");
            map = new HashMap<>();
            map.put("pic", file.getAbsolutePath());
            map.put("path", file.getPath());
            map.put("name", file.getName());
            map.put("audio", "pic");
            listrecys.add(map);
            myAddressReClass.setUpdate(listrecys);
        } else if (requestCode == 200) {
            map = new HashMap<>();
            map.put("path", data.getStringExtra("file"));
            map.put("name", data.getStringExtra("name"));
            map.put("pic", data.getStringExtra("pic"));
            map.put("audio", "video");
            listrecys.add(map);
            myAddressReClass.setUpdate(listrecys);
        }
    }


   public interface InterfaceFile {
        void getFilename(String filepath, String filename);


    }

    @Override
    public void initIalizeView() {
        ButterKnife.inject(this);
        ExitActivity.getInstance().addActivity(this);
        dialogs = new DialogLoading(MyAddRessRepair.this, name, true);
        netAddRess = new NetAddRess(handler);
        listrecys = new ArrayList<>();
        recyRepairsPho.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
        mode = UtlisNetWork.getnetwokstate(MyAddRessRepair.this);
        screenWidth = ScreenAsset.getScreenWidth(MyAddRessRepair.this);
        sharedPreferActivity = new SharedPreferActivity(this);
        tetitle.setText("报修");
        Intent intent = getIntent();
        //保修的种类
        fixCategory = intent.getIntExtra("Fixcategory", 0);
        modell = new FixModel(this);
        netAddRess.getGoodsList(modell.parseType(fixCategory), category);
        Spinner[] spArr = new Spinner[]{addressSelectAddress, addressSelectXiaoQu, addressSelectLouMen, addressSelectHuHao};
        myAddressReClass = new MyAddressReClass(this, this);//presenter层来实现业务处理
        model = new AddressModel(this, handler, spArr);
        model.setBackListener(new AddressModel.BackListener() {
            @Override
            public void Click(int type, int gi_id) {
                addressEt.setVisibility(View.GONE);
                switch (type) {
                    //代表点击了地区
                    case 1:
                        model.getNetData(0, gi_id, "", "village", "");
                        break;
                    case 2:
                        model.getNetData(0, gi_id, "", "unit", "");
                        break;
                    case 3:
                        model.getNetData(0, gi_id, "", "numbers", "");
                        break;
                }
            }


            @Override
            public void Put(boolean isVisible) {
                initHidden();
                if (isVisible == true) {
                    addressEt.setVisibility(View.VISIBLE);
                } else {
                    addressEt.setVisibility(View.GONE);
                }

            }
        });
        //录音成功时回调
        AudioRecorder audio = new AudioRecorder(new InterfaceFile() {
            @Override
            public void getFilename(String filepath, String filename) {
                map = new HashMap<>();
                map.put("path", filepath);
                map.put("name", filename);
                map.put("audio", "audio");
                listrecys.add(map);
                myAddressReClass.setUpdate(listrecys);
            }
        });
        imageRepairsYuyins.setAudioRecord(audio);

    }

    @Override//销毁页面停止语音
    protected void onDestroy() {
        super.onDestroy();
    }

    //组建的隐藏和显示
    private void initHidden() {
        switch (model.getClickType()) {
            case 0:
                lnXiaoQu.setVisibility(View.GONE);
                lnLouMen.setVisibility(View.GONE);
                lnHuHao.setVisibility(View.GONE);
                break;
            case 1:
                lnXiaoQu.setVisibility(View.VISIBLE);
                lnLouMen.setVisibility(View.GONE);
                lnHuHao.setVisibility(View.GONE);
                break;
            case 2:
                lnXiaoQu.setVisibility(View.VISIBLE);
                lnLouMen.setVisibility(View.VISIBLE);
                lnHuHao.setVisibility(View.GONE);
                break;
            case 3:
                lnXiaoQu.setVisibility(View.VISIBLE);
                lnLouMen.setVisibility(View.VISIBLE);
                lnHuHao.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }


}
